package com.syq.service.impl;

import com.syq.mapper.UserDao;
import com.syq.pojo.User;
import com.syq.service.UserService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @ClassName: UserServiceImpl
 * @Description: TODO
 * @Create by: syq
 * @Date: 4/7/2022 下午5:44
 */


public class UserServiceImpl implements UserService {


    @Override
    public User find(String username, String pwd) {
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("找不到配置文件");
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao userMapper = sqlSession.getMapper(UserDao.class);
        return userMapper.select(username, pwd);
    }

    @Override
    public void add(User user) {
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("找不到配置文件");
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao userMapper = sqlSession.getMapper(UserDao.class);
        userMapper.insert(user);
        sqlSession.commit();
        sqlSession.close();
    }
}
